<?php
  //----------------------------------------------------------------------
  // Sophia
  // Sophia by Alex Olave - http://www.alfazeta.cl
  //----------------------------------------------------------------------

  //----------------------------------------------------------------------
  // Author: 	Alex Olave - http://www.alfazeta.cl
  // Copyright: copyright (C) 2010 - Alex Olave.
  // License: 	GNU/GPL, http://www.gnu.org/copyleft/gpl.html
  // Pack: 	Sophia
  // File: 	util.helper.php
  //----------------------------------------------------------------------

  //----------------------------------------------------------------------
  // Sophia is free software. This version may have been modified pursuant
  // to the GNU General Public License, and as distributed it includes or
  // is derivative of works licensed under the GNU General Public License or
  // other free or open source software licenses.
  //----------------------------------------------------------------------


defined( '_JEXEC' ) or die( 'Restricted access' );

class nomHelperUtil {
	function createJS($array, $name) {
            $document = &JFactory::getDocument();
            $script = " var ".$name." = [";
            $i = 0;
            foreach($array as $a) {
                if ($i > 0) $script .= ',';
                $i++;
                $script .= "'".$a->value."'";
            }
            $script .= "];";
            $document->addScriptDeclaration($script);

/*            $script = ' var dependences = new Array(3);';
            foreach($array as $a) {
                $script .= " dependences[0] = '".$a->value."';";
            }
            echo $script;*/
            $document->addScriptDeclaration($script);
        }

    function loadFunctionCheck() {
       $checkscript = "
            function checkform() {
                var error = 0;
                var fin = 0;
                var inputs = $$('input');
                for (var i = 0; i < inputs.length; i++) {
                    var class = inputs[i].className;
                    if (class != '') {
                        var classes = class.split('_');
                        if (classes[0] == 'input') {
                            if (classes[1] == 'static') {
                                ids = inputs[i].id.split('_');
                                var array = eval('array_auto_'+ids[2]);
                                if (array.indexOf(inputs[i].value) == -1) {
                                    Sexy.alert('".JText::_('Error, you can not add values to static list')."');
                                    error = 1;
                                }
                            }
                            if (classes[1] == 'dinamic') {
                                ids = inputs[i].id.split('_');
                                var array = eval('array_auto_'+ids[2]);
                                if ((array.indexOf(inputs[i].value) == -1) && (inputs[i].value != '')) {
                                    error = 1;
                                    Sexy.confirm('".JText::_('The value').": ' + inputs[i].value + '".JText::_('dont exists, do you want add it?')."', {
                                        textBoxBtnOk: '".JText::_('Yes')."',
                                        textBoxBtnCancel: '".JText::_('No')."',
                                        onComplete:
                                            function(returnvalue) {
                                                if (returnvalue) {
                                                    if (fin == 1) {
                                                        document.note.submit();
                                                    }
                                                } else {
                                                    Sexy.alert('".JTEXT::_('Change it')."');
                                                    error = 1;
                                                }
                                            }
                                    });
                                }
                            }

                            if (classes[2] == '1') {
                                if (inputs[i].value == '') {
                                    Sexy.alert('".JText::_('Error, empty obligatory field')."');
                                    error = 1;
                                }
                            }
                        }
                    }
                }
                fin = 1;
                if (error == 0) {
                    document.note.submit();
                }
            }
        ";

        return $checkscript;        
    }
	 
}

?>
